package com.task.base;

import java.util.Comparator;

public class ProcessSortComparator implements Comparator<ProcessItem> {
	
	public static final int sortByCpu=0;
	
	public static final int sortByMemory=1;
	
	int mySortBY = ProcessSortComparator.sortByCpu;// 0 表示cpu排序，1表示按内存排序.

	public ProcessSortComparator(int sortBy) {
		mySortBY = sortBy;
	}

	public void setSortBy(int sortBy)
	{
		mySortBY=sortBy;
	}
	@Override
	public int compare(ProcessItem p1, ProcessItem p2) {

		if (mySortBY == 0) {
			long resultCpu = p1.cupValue - p2.cupValue;
			if (resultCpu > 0) {
				resultCpu = -1;
			} else if (resultCpu < 0) {
				resultCpu = 1;
			}else{
				resultCpu=0;
			}
			return (int) resultCpu;
		} else {
			long result = p1.memValue - p2.memValue;
			if (result > 0) {
				result = -1;
			} else if (result < 0) {
				result = 1;
			}else{
				result=0;
			}
			return (int) result;

		}

	}

}
